// pages/video.js
import {
  getVideoData
} from '../../service/api'
import Toast from '../../miniprogram_npm/vant-weapp/toast/toast'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    hasMore: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData(0)
  },
  onPullDownRefresh() {
    this.getData(0)
    wx.stopPullDownRefresh()
  },
  onReachBottom() {
    this.getData(this.data.list.length)
  },
  async getData(offset) {
    if (!this.data.hasMore && offset !== 0) return wx.showToast({
      title: '没有更多了~',
      icon: 'none'
    })
    Toast.loading({
      message: '数据加载中~',
      loadingType: 'spinner'
    })
    const res = await getVideoData(offset)
    if (offset == 0) {
      this.setData({
        list: res.data
      })
    } else {
      this.setData({
        list: this.data.list.concat(res.data)
      })
    }
    this.setData({
      hasMore: res.hasMore
    })
    Toast.clear()
  }, // 获取数据method
  goDetail(event) {
    wx.navigateTo({
      url: `/pages/detail/detail?id=${event.currentTarget.dataset.item.id}`,
    })
  }
})